<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>ajax  get练习</h1>
    <form id="form">
        <input id="ipt" type="text" name="name">
        <button id="btn">发送请求</button>
    </form>
    <script>
        const oBtn = document.querySelector('#btn')
        const oIpt = document.querySelector('#ipt')
        const oForm = document.querySelector('#form')
        
        oForm.onsubmit = function(){
            // 获取用户输入信息
            const userValue = oIpt.value

            // 创建ajax实例
            const xhr = new XMLHttpRequest();
            // query传参
            xhr.open('GET',`/user?userId=${userValue}&_=${Date.now()}`,true)
            //如果用path传参，服务器的接口：/user/:userID
            // xhr.open("GET", `/user/${userId}?_=${Date.now()}`, true)
            
            xhr.send();

            xhr.onreadystatechange  = function(){
                if(xhr.readyState ===4 && xhr.status>=200&&xhr.status<=299){
                    
                    console.log( xhr.responseText);
                }
            }
            // 终止表单自动提交
            return false
        }
    </script>

</body>
</html>